package com.cz.nongyetongv5.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.BlueLoadingProgress;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class TongXunLuStatisticsActivity extends Activity implements
		OnClickListener {

	private WebView wvStart;

	private Button btnLeft;

	private Button btnRight;

	private User user;

	private ViewGroup btnBack;

	private BlueLoadingProgress blpLoading = null;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.tongxunlu_start);

		wvStart = (WebView) findViewById(R.id.wvStart);

		btnLeft = (Button) findViewById(R.id.btnLeft);

		btnLeft.setBackgroundColor(getResources().getColor(R.color.title_color));

		btnRight = (Button) findViewById(R.id.btnRight);

		btnLeft.setOnClickListener(this);

		btnRight.setOnClickListener(this);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		blpLoading = (BlueLoadingProgress) findViewById(R.id.blpLoading);
		blpLoading.setVisibility(View.GONE);

		user = (User) getIntent().getSerializableExtra("user");

		// 存在网络的情况下

		// 启动javascr
		wvStart.getSettings().setJavaScriptEnabled(true);

		wvStart.getSettings().setLoadsImagesAutomatically(true);

		wvStart.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {

				blpLoading.setVisibility(View.VISIBLE);

				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {

				if (blpLoading != null) {
					blpLoading.stopLoading();
					blpLoading.setVisibility(View.GONE);
				}

				super.onPageFinished(view, url);
			}

		});
		//

		String url = new ConfigUtil(getResources()).getPreHttpUrl()
				+ getString(R.string.tongxunlu_count_url)
				+ user.getKnowRegion();

		wvStart.loadUrl(url);

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		// final Button btn02 = (Button) pop.getContentView().findViewWithTag(
		// "btn1");
		//
		// final Button btn03 = (Button) pop.getContentView().findViewWithTag(
		// "btn2");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}

				// if (v == btn02) {
				//
				// GregorianCalendar calendar = new GregorianCalendar();
				// calendar.add(Calendar.YEAR, -1);
				//
				// String year = calendar.get(Calendar.YEAR) + "";
				//
				// String url = new ConfigUtil(getResources()).getPreHttpUrl()
				// + getString(R.string.tongxunlu_count_url)
				// + user.getKnowRegion() + "/year/" + year;
				//
				// Log.d("url~~", url);
				//
				// wvStart.loadUrl(url);
				//
				// titleState.setText("当前数据:去年");
				// }
				//
				// if (v == btn03) {
				// String url = new ConfigUtil(getResources()).getPreHttpUrl()
				// + getString(R.string.tongxunlu_count_url)
				// + user.getKnowRegion();
				//
				// Log.d("url~~", url);
				//
				// wvStart.loadUrl(url);
				//
				// titleState.setText("当前数据:今年");
				// }

				pop.dismiss();

			}
		};

		btn01.setOnClickListener(menuOnClick);

		// btn02.setOnClickListener(menuOnClick);
		//
		// btn03.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {

		if (v == btnBack) {

			finish();

			return;
		}

		btnLeft.setBackgroundResource(R.drawable.btn_back_pressed);
		btnRight.setBackgroundResource(R.drawable.btn_back_pressed);

		if (v == btnLeft) {
			btnLeft.setBackgroundColor(getResources().getColor(
					R.color.title_color));

		} else if (btnRight == v) {
			btnRight.setBackgroundColor(getResources().getColor(
					R.color.title_color));

			Intent to_tongxunlu_intent = new Intent(this,
					TongXunLuActivity.class);

			// 传递到下一个页面
			to_tongxunlu_intent.putExtra("user", this.user);

			startActivity(to_tongxunlu_intent);

			btnLeft.setBackgroundColor(getResources().getColor(
					R.color.title_color));

			btnRight.setBackgroundResource(R.drawable.btn_back_pressed);
		}

	}

}
